package com.wn.carport.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.carport.param.CarportParm;
import com.wn.carport.param.CarportSearchParam;
import com.wn.carport.resp.PgRe;
import com.wn.carport.service.impl.CarPortImpl;
import com.wn.carport.util.Base64ToImageUtil;
import com.wn.carport.vo.CarPortAreaVillageVo;
import com.wn.common.entity.Const;
import com.wn.common.entity.MyParam;
import com.wn.common.entity.MyRsp;
import com.wn.common.pojo.*;
import com.wn.common.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @BelongsProject: shared_parking
 * @BelongsPackage: com.wn.carport.controller
 * @Author: 刘辉
 * @CreateTime: 2020-06-11 15:09
 * @Description:
 */

/**
 * @Slf4j:是对swagger的支持
 * @CrossOrigin:开启对跨域问题的解决
 */
@Api(description = "这是一个车位信息的接口")
@Slf4j
@RestController
public class CarPortController {

    private final static Logger logger = LoggerFactory.getLogger(CarPortController.class);



    @Autowired
    private CarPortImpl carPortImpl;

    @ApiOperation("根据车位主键id查询车位信息")
    @PostMapping("getCarPortById")
    public MyRsp getCarPortById(@RequestBody String cpId){

        CarPort carPortById = carPortImpl.getCarPortById(cpId);
        if (carPortById!=null){//查询出了
            return MyRsp.success(carPortById);
        }
        else {//没有查询出
            return MyRsp.error();
        }
    }

    @ApiOperation("逻辑删除车位信息")
    @PostMapping("deleteCarPort")
    public MyRsp deleteCarPort(@RequestBody CarPortAreaVillageVo carPortAreaVillageVo){

        Integer integer = carPortImpl.deleteCarPort(carPortAreaVillageVo.getCpId());
        if (integer>0){//逻辑删除成功
            return MyRsp.success1(Const.Status.STATUS_SUCCESS,"车位删除成功");
        }
        else {//逻辑删除失败
            return MyRsp.success1(Const.Status.STATUS_ERROR,"删除失败");
        }
    }


    @ApiOperation("添加车位信息")
    @PostMapping("addCarPort")
    public MyRsp addCarPort(@RequestBody CarPortAreaVillageVo carPortAreaVillageVo){

        System.out.println("前端添加的车位信息："+carPortAreaVillageVo);

        //1.获取到车位的base64主图字符串
        String cpMainImage = carPortAreaVillageVo.getCpMainImage();

        //2.字符串截取，拿到base64字符串
        int base64 = cpMainImage.indexOf("base64");
        String mainImageBase64 = cpMainImage.substring(base64 + 7);
        String uploadImage = FileUtil.uploadImage(mainImageBase64);

        //5.将访问地址保存到车位对象
        carPortAreaVillageVo.setCpMainImage(uploadImage);

        carPortAreaVillageVo.setAid(UUID.randomUUID().toString());

        //6.添加
        Integer addCartPort = carPortImpl.addCartPort(carPortAreaVillageVo);

        if (addCartPort>0){//添加成功
            return MyRsp.success1(Const.Status.STATUS_SUCCESS,"添加成功");
        }
        else {//失败
            return MyRsp.success1(Const.Status.STATUS_ERROR,"添加失败");
        }
    }

    @ApiOperation("根据区域名称查询id")
    @PostMapping("getvillageIdByVname")
    public MyRsp getvillageIdByVname(AreaVillage areaVillage){

        AreaVillage areaVillage1 = carPortImpl.getvillageIdByVname(areaVillage.getAName());
        if (areaVillage1!=null){//ok
            return MyRsp.success(areaVillage1);
        }
        else {//no
            return MyRsp.error();
        }
    }

    @ApiOperation("根据车位主键修改车位信息")
    @PostMapping("updateCarPort")
    public MyRsp updateCarPort(@RequestBody CarportParm carportParm){

        return carPortImpl.updateCarPort(carportParm);
    }


    @ApiOperation("查询车位列表分页")
    @PostMapping("getCarPolAll")
    public MyRsp getCarPolAll(@RequestBody MyParam<CarPort> myParam){

        PageInfo<CarPort> carPortlAll = carPortImpl.getCarPortlAll(myParam);

        return MyRsp.success(carPortlAll);

    }


    /**
     * 车位列表后台展示
     * @param page
     * @param rows
     * @return
     */
    @ApiOperation("查询车位列表分页003")
    @PostMapping("getCarPolAll003")
    public PgRe getCarPolAll003(Integer page,Integer rows,String aName){


        PgRe carPortAll03 = carPortImpl.getCarPortAll03(page, rows,aName);

        return carPortAll03;
    }

    /**
     *
     * @param cpId
     * @return
     */
    @ApiOperation("物理删除车位信息根据主键")
    @PostMapping("deleteCarPortPhy")
    public MyRsp deleteCarPortPhy(String cpId){

        Integer deleteCarPortPhy = carPortImpl.deleteCarPortPhy(cpId);
        if (deleteCarPortPhy>0){
            return MyRsp.success1(Const.Status.STATUS_SUCCESS,"删除成功");
        }
        else {
            return MyRsp.success1(Const.Status.STATUS_ERROR,"删除失败");
        }
    }

    @ApiOperation("车位高级检索c")
    @PostMapping("superSearch")
    public MyRsp superSearch(String superSearchKeyWord){
        List<CarPort> carPorts = carPortImpl.superSearch(superSearchKeyWord);
        if (carPorts!=null){//ok
            return MyRsp.success(carPorts).code(0);
        }
        else {//no
            return MyRsp.success1(Const.Status.STATUS_ERROR,"搜索失败");
        }
    }




    /**
     * 根据地区id查询小区
     * @param aid
     * @return
     */
    @ApiOperation("根据地区id获取小区数据")
    @RequestMapping("getVillageByAreaId")
    public List<AreaVillage> getVillageByAreaId(String aid){
        System.out.println(aid);

        List<AreaVillage> villageByAreaId = carPortImpl.getVillageByAreaId(aid);

        return villageByAreaId;
    }

    @ApiOperation("获取地区前台")
    @PostMapping("getArea")
    public MyRsp getArea(){

        List<AreaVillage> areaAll = carPortImpl.getAreaAll();
        if (areaAll!=null){//ok
            return MyRsp.success(areaAll);
        }
        else {//no
            return MyRsp.error();
        }
    }
    @ApiOperation("获取地区001")
    @PostMapping("getAreaAll01")
    public List<AreaVillage> getAreaAll01(){

        List<AreaVillage> areaAll01 = carPortImpl.getAreaAll01();

        return areaAll01;
    }

    @ApiOperation("根据地区名称找到下面的所有小区")
    @PostMapping("getVillage")
    public MyRsp getVillage(@RequestBody String aName){

        List<AreaVillage> villageByAreaName = carPortImpl.getVillageByAreaName(aName);

        if (villageByAreaName==null){//no

            return MyRsp.success1(Const.Status.STATUS_ERROR,"查询失败");
        }
        else {//ok

            return MyRsp.success(villageByAreaName);
        }
    }

    /**
     * 区域表/车位表联合查询
     * @param aName
     * @return
     */
    @ApiOperation("区域表/车位表联合查询小区内的车位")
    @PostMapping("getCarPortByVillage")
    public MyRsp getCarPortByVillage(@RequestBody String aName){

        List<CarPort> carPortByVillage = carPortImpl.getCarPortByVillage(aName);
        if (carPortByVillage==null){//no

            return MyRsp.success1(Const.Status.STATUS_ERROR,"查询异常");
        }
        else {//ok
            return MyRsp.success(carPortByVillage);
        }
    }

    /*根据小区名称模糊查询车位*/
    @ApiOperation("根据小区名称模糊查询")
    @PostMapping("getCarPortByAreaVillageName")
    public MyRsp getCarPortByAreaVillageName(String aName){

        System.out.println(aName);

        List<CarPortAreaVillageVo> carPortByAreaVillageName = carPortImpl.getCarPortByAreaVillageName(aName);
        if (carPortByAreaVillageName!=null){//ok
            return MyRsp.success(carPortByAreaVillageName);
        }
        else {//no
            return MyRsp.error();
        }

    }

    /*车位的上下架*/
    @ApiOperation("车位上下架")
    @PostMapping("upDownShelf")
    public MyRsp upDownShelf(@RequestBody String cpId,@RequestBody Integer cpStatus){

        Integer upDownShelf = carPortImpl.upDownShelf(cpId, cpStatus);
        if (upDownShelf>0){//ok
            return MyRsp.success1(Const.Status.STATUS_SUCCESS,"ok");
        }
        else {//no
            return MyRsp.error();
        }
    }

    /**
     * 根据车位id获取到地区名称
     * @param cpId
     * @return
     */
    @ApiOperation("根据车位id获取到地区名称")
    @PostMapping("getAreaNameByCpId")
    public MyRsp getAreaNameByCpId(@RequestBody String cpId){

        AreaVillage areaNameByCarPortId = carPortImpl.getAreaNameByCpId(cpId);
        System.out.println(areaNameByCarPortId+"***");
        if (areaNameByCarPortId!=null){//ok

            return MyRsp.success(areaNameByCarPortId.getAName());
        }
        else {//no
            return MyRsp.error();
        }
    }

    @ApiOperation("前台根据车位id获取车位地区/价格 vo信息")
    @PostMapping("getCarPortAreaVo")
    public MyRsp getCarPortAreaVo(@RequestBody CarPortAreaVillageVo carPortAreaVillageVo){

        CarPortAreaVillageVo carPortAreaVo = carPortImpl.getCarPortAreaVo(carPortAreaVillageVo.getCpId());

        if (carPortAreaVo!=null){//ok
            return MyRsp.success(carPortAreaVo);
        }
        else {//no
            return MyRsp.error();
        }
    }

    @ApiOperation("根据地区名称查看小区 集合")
    @PostMapping("getAreaVillagedo")
    public List<AreaVillage> getAreaVillagedo(String aName){

        List<AreaVillage> areaVillagedo = carPortImpl.getAreaVillagedo(aName);
        /*System.out.println("*****"+areaVillagedo);*/
        return areaVillagedo;
    }

    @ApiOperation("车位id -》车位主图/价格/地区/小区")
    @PostMapping("getCarportVo")
    public MyRsp getCarportVo(@RequestBody CarPortAreaVillageVo carPortAreaVillageVo){

        /**/
        CarPortAreaVillageVo carportVo = carPortImpl.getCarportVo(carPortAreaVillageVo.getCpId());
        System.out.println("ada"+carportVo.getVName());
        AreaVillage areaName = carPortImpl.getAreaName(carportVo.getVName());

        String aName = areaName.getAName();

        carportVo.setAName(aName);
        /************/
        if (carportVo!=null&&areaName!=null){//ok
            return MyRsp.success(carportVo);
        }
        else {//no
            return MyRsp.error();
        }
    }




    @ApiOperation("根据小区名称或者关键字查询这个小区下面所有的车位")
    @PostMapping("getListByVnameOrKeyword")
    public MyRsp getListByVnameOrKeyword(@RequestBody CarportSearchParam carportSearchParam){


        return carPortImpl.getListByVnameOrKeyword (carportSearchParam);
    }


    //查询所有车位 支持多条件分页排序查询
    @ApiOperation("支持分页查询所有车位")
    @PostMapping("getAllCarPort")
    public Object getAllCarPort(MyParam<CarPort> param){


        return carPortImpl.getAllCarPort(param);
    }

    @ApiOperation("管理员查询所有车位")
    @PostMapping("carPortList")
    public Object carPortList(Integer page,Integer rows,Integer cpCode,String superSearchKeyWord) {
        logger.info("cpCode,{}",cpCode);
        logger.info("superSearchKeyWord,{}",superSearchKeyWord);
        return carPortImpl.carPortList(page,rows,cpCode,superSearchKeyWord);
    }


    @ApiOperation("展示物业方的车位信息")
    @PostMapping("carPortListByProperty")
    public Object carPortListByProperty(Integer page,Integer rows,Integer cpCode,String superSearchKeyWord,@CookieValue(value = "token",defaultValue = "---") String token){
        //获取物业id
        return carPortImpl.carPortListByProperty(page, rows,cpCode,superSearchKeyWord, token);
    }

    @ApiOperation("展示出租方的车位信息")
    @PostMapping("carPortListByLessor")
    public Object carPortListByLessor(Integer page,Integer rows,Integer cpCode,String superSearchKeyWord,@CookieValue(value = "token",defaultValue = "---") String token){
        //获取物业id
        return carPortImpl.carPortListByLessor(page, rows,cpCode,superSearchKeyWord, token);
    }



    @ApiOperation("添加车位")
    @PostMapping("addCarport")
    public MyRsp addCarport(@RequestBody CarportParm carportParm,@CookieValue(value = "token",defaultValue = "---") String token){

        return carPortImpl.addCarport(carportParm,token);
    }

    @ApiOperation("出租客添加车位")
    @PostMapping("addCarportByLessor")
    public MyRsp addCarportByLessor(@RequestBody CarportParm carportParm,@CookieValue(value = "token",defaultValue = "---") String token){

        return carPortImpl.addCarportByLessor(carportParm,token);
    }


    @ApiOperation("批量逻辑删除车位")
    @PostMapping("deleteSomeCarport")
    public MyRsp deleteSomeCarport(@RequestBody  String[] ids){

        return carPortImpl.deleteSomeCarport(ids);
    }


}
